package hongcaosp.app.android.video.player;

import android.content.Context;

import java.util.HashMap;
import java.util.Map;

import hongcaosp.app.android.video.PlayConstant;

public class PlayVideoManager {

    private static PlayVideoManager instance;

    private Map<Object,VideoProxy> videoContext = new HashMap<>();

    private PlayVideoManager() {

    }

    public static PlayVideoManager getInstance() {
        synchronized (PlayVideoManager.class) {
            if (instance == null) {
                instance = new PlayVideoManager();
            }
            return instance;
        }
    }

    public void onPlay(VideoProxy simplePlayerView,Object obj) {
        VideoProxy currentPlayer = videoContext.get(obj);
        if (currentPlayer == simplePlayerView) return;
        if (currentPlayer != null) {
            currentPlayer.stopVideo();
        }
        videoContext.put(obj,simplePlayerView);
    }

    public void start(Object obj) {
        if (videoContext.get(obj) == null) {
            return;
        }
        videoContext.get(obj).playVideo();
    }

    public void stop(Object obj) {
        if (videoContext.get(obj) == null) {
            return;
        }
        videoContext.get(obj).stopVideo();
    }

    public void destroy(Object obj) {
        if (videoContext.get(obj) != null) {
            videoContext.get(obj).stopVideo();
        }
        videoContext.remove(obj);
    }

    public void onHide(Object obj) {
        PlayConstant.isPlayOk = false;
        if (videoContext.get(obj) == null) {
            return;
        }
        videoContext.get(obj).pauseVideo();
    }

    public void onShow(Object obj) {
        PlayConstant.isPlayOk = true;
        if (videoContext.get(obj) == null) {
            return;
        }
        videoContext.get(obj).resumeVideo();
    }

}
